package com.solotiger.geographic.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.solotiger.geographic.biz.model.Picture;
import com.solotiger.geographic.db.DatabaseManger;
import com.solotiger.geographic.ui.view.CollectHolderView;

import java.util.List;

/**
 * Adapter
 */
public class CollectAdapter extends BaseAdapter {

    private List<Picture> datas;

    private Context context;

    public CollectAdapter(Context context, List<Picture> datas) {
        this.datas = datas;
        this.context = context;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Picture getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        CollectHolderView holder;
        if (convertView != null) {
            holder = (CollectHolderView) convertView;
        } else {
            holder = new CollectHolderView(context);
        }

        holder.setData(getItem(position), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseManger.ME.deleteItem(getItem(position).getId());
                notifyDataSetChanged();
            }
        });
        return holder;
    }
}
